<?php

namespace App\Http\Controllers\admin\users;

use App\Http\Controllers\Controller;
use App\Models\admin\AdminUserModel;
use App\Models\users\UsersAuthenticationModel;
use App\Models\users\UsersModel;
use App\Models\users\UsersWithdrawOrderModel;
use App\Service\users\UsersService;
use App\Service\users\WithdrawService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class UsersController extends Controller
{

    /**
     * 获取用户列表页面数据
     */
    public function getUsersListData(): JsonResponse
    {
        $data = request()->input();

        $query = UsersModel::select('users.*');

        if ($data['keywords'] != '') {
            $query->where(function ($query) use ($data) {
                $query->where('users.nickname', 'like', '%' . $data['keywords'] . '%')
                    ->orWhere('users.mobile', 'like', '%' . $data['keywords'] . '%');
            });
        }

        if ($data['dateTime'] != '') {
            foreach ($data['dateTime'] as $k => $v) {
                $data['dateTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = strtotime($data['dateTime'][0]);
            $endTime = strtotime($data['dateTime'][1]);
            $query->whereBetween('users.create_time', [$startTime, $endTime]);
        }

        if ($data['uid'] != '') {
            $uid = $data['uid'];
            $query->where('users.id', $uid);
        }

        $lists = $query->orderBy('id', 'desc')->paginate($data['pageSize']);
        foreach ($lists as $v) {
            $v->create_time = Tools::timeConvert($v->create_time, 'Y-m-d');
        }

        return ApiResponse::apiResponseData(200, '获取用户列表数据成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 获取用户详情
     */
    public function getUsersInfoData(): JsonResponse
    {
        $uid = request()->input('id');

        $lists = UsersService::instance()->getUserDetail($uid);

        return ApiResponse::apiResponseData(200, '获取用户详情成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 搜索获取用户信息列表
     */
    public function getUsersSearchListData(): JsonResponse
    {
        $data = request()->input('value');

        $getUserList = UsersModel::where('nickname', 'like', '%' . $data . '%')
            ->orwhere('mobile', 'like', '%' . $data . '%')
            ->select('id', 'nickname', 'avatar', 'mobile')
            ->get();

        return ApiResponse::apiResponseData(200, '获取订单分页列表成功', [
            'getUserList' => $getUserList,
        ]);
    }


    /**
     * 用户表单提交
     */
    public function usersFormSubmit(): JsonResponse
    {
        $form = request()->post();

        if (isset($form['id'])) {
            $id = $form['id'];
            $data['is_cancel'] = $form['is_cancel'];
            if ($data['is_cancel'] == 1) {
                $data['cancel_time'] = time();
            }
            unset($form);
            $save = UsersModel::where('id', $id)->update($data);
            if ($save) {
                return ApiResponse::apiResponseData(200, '用户提交成功');
            }
            return ApiResponse::apiResponseData(400, '用户提交失败');
        }
        return ApiResponse::apiResponseData(400, '用户提交失败');
    }


    /**
     * 获取用户提现记录列表页面数据
     */
    public function getUsersWithdrawListData(): JsonResponse
    {
        $data = request()->input();

        $query = UsersWithdrawOrderModel::leftJoin('users', function ($join) {
            $join->on('users_withdraw_order.uid', '=', 'users.id');
        });

        if ($data['keywords'] != '') {
            $query->where(function ($query) use ($data) {
                $query->where('users.nickname', 'like', '%' . $data['keywords'] . '%')
                    ->orWhere('users.mobile', 'like', '%' . $data['keywords'] . '%');
            });
        }

        if ($data['status'] != '') {
            $query->where('users_withdraw_order.status', $data['status']);
        }

        if ($data['type'] != '') {
            $query->where('users_withdraw_order.type', $data['type']);
        }

        if ($data['dateTime'] != '') {
            foreach ($data['dateTime'] as $k => $v) {
                $data['dateTime'][$k] = (new Carbon($v))->format('Y-m-d H:i:s');
            }
            $startTime = strtotime($data['dateTime'][0]);
            $endTime = strtotime($data['dateTime'][1]);
            $query->whereBetween('users_withdraw_order.create_time', [$startTime, $endTime]);
        }


        $lists = $query->select('users_withdraw_order.*', 'users.nickname', 'users.avatar', 'users.mobile')
            ->orderBy('users_withdraw_order.id', 'desc')
            ->paginate($data['pageSize']);
        if ($lists) {
            foreach ($lists as $v) {
                $v->type = UsersWithdrawOrderModel::getwithdrawType($v->type);
                $v->commission = $v->commission * 100 . '%';
                $v->examine_time = Tools::timeConvert($v->examine_time);
                $v->create_time = Tools::timeConvert($v->create_time);
            }
        }

        return ApiResponse::apiResponseData(200, '获取用户提现记录列表页面数据成功', [
            'lists' => $lists
        ]);
    }


    /**
     * 用户提现记录驳回提交
     */
    public function usersWithdrawRejectSubmit(): JsonResponse
    {
        $form = request()->post();

        $username = AdminUserModel::where('id', Request()->system_uid)->value('username');

        $result = WithdrawService::instance()->withdrawRejectHandle($form['id'], $form['examine_reason'], $username);
        if ($result) {
            return ApiResponse::apiResponseData($result['code'], $result['message']);
        }
        return ApiResponse::apiResponseData(404, 'service错误');
    }


    /**
     * 打款用户提交
     */
    public function usersWithdrawPaySubmit()
    {
        $withdraw_id = request()->post('id');
    }


    /**
     * 获取用户实名认证列表页面数据
     */
    public function getUsersRealNameListData(): JsonResponse
    {
        $data = request()->input();

        $query = UsersAuthenticationModel::select('users_authentication.*', 'users.nickname', 'users.avatar')
            ->leftJoin('users', function ($join) {
                $join->on('users_authentication.uid', '=', 'users.id');
            });

        if (isset($data['uid']) && $data['uid'] != '') {
            $query->where('uid', $data['uid']);
        }

        $lists = $query->orderBy('update_at', 'desc')->paginate($data['pageSize']);

        return ApiResponse::apiResponseData(200, '获取用户实名认证列表页面数据成功', [
            'lists' => $lists,
        ]);
    }


    /**
     * 用户实名认证审核提交
     */
    public function usersRealNameAuditSubmit(): JsonResponse
    {
        $form = request()->post();
        $save = UsersAuthenticationModel::where('id', $form['id'])->update([
            'status' => $form['status'],
            'audit_time' => time(),
            'audit_admin' => AdminUserModel::where('id', Request()->system_uid)->value('username'),
        ]);
        if ($save) {
            return ApiResponse::apiResponseData(200, '操作成功');
        }
        return ApiResponse::apiResponseData(400, '操作失败');
    }

}
